import os
import subprocess

# 优先安装torch，因为其他一些包依赖它
print('Installing torch first...')
import subprocess
subprocess.run(['pip', 'install', 'torch>=2.2.0'], check=True)

# 然后安装其他依赖
print('Installing other dependencies...')
with open('requirements.txt', 'r', encoding='utf-8') as f:
    requirements = f.read().splitlines()
    
for req in requirements:
    # 跳过空行和注释行
    if req.strip() == '' or req.strip().startswith('#'):
        continue
    
    # 移除行中的注释部分
    if '#' in req:
        req = req.split('#')[0].strip()
    
    # 跳过Python版本要求和已经安装的torch
    if 'python>=' in req.lower() or 'torch>=' in req.lower():
        continue
    
    print(f'Installing: {req}')
    try:
        subprocess.run(['pip', 'install', req], check=True)
    except subprocess.CalledProcessError:
        print(f'Warning: Failed to install {req}, skipping...')

print('Most dependencies installed successfully!')